class TalksController < ApplicationController
  before_filter :authorize, :except=>[:signup,:signin,:reset_password]
#  in_place_edit_for :talkee, :body
#  protect_from_forgery :except => [:set_talkee_body]
  # GET /talks
  # GET /talks.xml
  def index
    
    @user_talks = @user.user_talks.sort_by{|user_talk| [user_talk.star, user_talk.talk.latest_update_at]}.reverse
    #@user_talks = @user.user_talks.sort_by{|user_talk| user_talk.star}
    #@user_talks0 = @user.user_talks.sort_by{|user_talk| user_talk.talk.latest_update_at(user_talk.talk)}.reverse
    #@user_talks1 = @user_talks0.find_all(:star => true)
    #@user_talks << @user_talks1
    #@user_talks2 = @user_talks0.find(:all, :star => false)
    #@user_talks << @user_talks2
    #@talks = Talk.all
    #@talks = @user.talks.sort_by{|talk| [talk.star(talk,@user), talk.latest_update_at(talk)]}.reverse
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @talks }
    end
  end
  
  # GET /talks/1
  # GET /talks/1.xml
  def show
    #@user = User.find(session[:user_id])
    @talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])
    @talkees = @talk.talkees
    session[:talk_id] = params[:id] if params[:id]
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @talk }
    end
  end
  
  def show_this_talker_only
    @talker = User.find(params[:id])
    @talk = Talk.find(session[:talk_id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @talker.id, @talk.id ])
    @talkees = @talk.talkees.find(:all, :conditions => [ "user_id = ?", @talker.id])

    respond_to do |format|
      format.html { render :action => "show" }
      format.xml  { render :xml => @talk }
    end
  end
  
  def show_this_branch_only
    @talkee = Talkee.find(params[:id])
    @talk = Talk.find(session[:talk_id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])
    @talkees = [@talkee]
    if @talkee.flag
      @master = Talkee.find(@talkee.talkee_id)
      @talkees << @master
      while(@master.flag) do
        @master = Talkee.find(@master.talkee_id)
        @talkees << @master
      end
    end
    @talkees = @talkees.reverse
    #@talkees = @talk.talkees.find(:all, :conditions => [ "user_id = ?", @talker.id])

    respond_to do |format|
      format.html { render :action => "show" }
      format.xml  { render :xml => @talk }
    end
  end
  # GET /talks/new
  # GET /talks/new.xml
  def new_talk
    @talk = Talk.new(params[:talk])
    flash[:notice] = info(0,"Talk is successfully created.")
    @talk.created_by = @user.id
    @talk.save

    @talk.users << @user

    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])

  end
  
  # GET /talks/1/edit
  def edit
    @talk = Talk.find(params[:id])
  end
  
  # POST /talks
  # POST /talks.xml

  
  def new_talkee
    @talk = Talk.find(params[:talk_id]) #can't use session, cause problem if opening multiple windows
    @new_talkee = @talk.talkees.build(params[:talkee])
    @new_talkee.user = @user
    @new_talkee.save
    flash[:notice] = info(0,"Talkee is successfully created.")
  end
  
  def reply_to_talkee
    @talk = Talk.find(params[:talk_id])
    @master_talkee = Talkee.find(params[:master_talkee_id])
    @new_talkee = @talk.talkees.build(params[:talkee])

    @new_talkee.user = @user
    @new_talkee.flag = 1
    @new_talkee.talkee = @master_talkee
    @new_talkee.save
    @master_talkee.follows << @new_talkee
    flash[:notice] = info(0,"Talkee is successfully created.")
  end
  # PUT /talks/1
  # PUT /talks/1.xml
  def update
    @talk = Talk.find(params[:id])
    
    respond_to do |format|
      if @talk.update_attributes(params[:talk])
        flash[:notice] = info(0,"Talk is successfully updated.")
        format.html { redirect_to(@talk) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @talk.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /talks/1
  # DELETE /talks/1.xml
  def destroy
    @talk = Talk.find(params[:id])
    @talk.destroy
    
    respond_to do |format|
      format.html { redirect_to(talks_url) }
      format.xml  { head :ok }
    end
  end
  
  def change_star
    #@talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(params[:id])

    @user_talk.toggle!(:star)
    respond_to do |format|
        flash[:notice] = info(0,"Talk Star is successfully switched.")
        format.html { redirect_to(talks_url) }
        format.xml  { head :ok }
    end
  end
  
  def change_star_show
    #@talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(params[:id])

    @user_talk.toggle!(:star)
    respond_to do |format|
        flash[:notice] = info(0,"Talk Star is successfully switched.")
        format.html { redirect_to(talk_url(session[:talk_id])) }
        format.xml  { head :ok }
    end
  end
  
  def email_talk_history
    @talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])
    @talkees = @talk.talkees
    email = Mailer.create_talk_history(@talk,@user.email)
    email.set_content_type("text/html" )
    Mailer.deliver(email)
    #Mailer.deliver_talk_history(@talk,@user.email)
     
    flash[:notice] = info(0,"Talk is successfully send to #{@user.email}.")
    respond_to do |format|
      format.html { redirect_to(talk_url(session[:talk_id]))}
      format.xml  { render :xml => @talk }
    end
  end
    
  def delete_talkee
    @talkee = Talkee.find(params[:id])
    @talkee.deleted = ! @talkee.deleted
    @talkee.save
    @talk = Talk.find(params[:talk_id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])
    @talkees = @talk.talkees

    respond_to do |format|
      format.html { redirect_to(talk_url(session[:talk_id])) }
      format.xml  { render :xml => @talk }
    end
  end
  
  def update_talkee
    @talk = Talk.find(params[:talk_id])
    @update_talkee = Talkee.find(params[:update_talkee_id])

      if @update_talkee.update_attributes(params[:talkee])
        flash[:notice] = info(0,"Talkee is successfully created.")
      else
        format.html { render :action => "edit" }
      end
  end

  def update_talk_body
    @talk = Talk.find(params[:talk_id])
    @update_talk = @talk

      if @update_talk.update_attributes(params[:talk])
        flash[:notice] = info(0,"Talk is successfully created.")
      else
        format.html { render :action => "edit" }
      end
  end
  
  def quit_talk
    @talk = Talk.find(params[:id])
    @user_talk = UserTalk.find(:first, :conditions => [ "user_id = ? AND talk_id = ?", @user.id, @talk.id ])
    @user_talk.status = 1
    @user_talk.save
    respond_to do |format|
      format.html { redirect_to(talks_path) }
      format.xml  { render :xml => @talk }
    end
  end
end
